package org.dreamwork.android.websocket.impls.okhttp_3_6_0;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import org.dreamwork.android.websocket.IWebSocketListener;

/**
 * Created by game on 2017/3/6
 */
class OkHttpWebSocketListenerDelegate extends WebSocketListener {
    private IWebSocketListener listener;

    OkHttpWebSocketListenerDelegate (IWebSocketListener listener) {
        this.listener = listener;
    }

    @Override
    public void onOpen (WebSocket webSocket, Response response) {
        listener.onOpen ();
    }

    @Override
    public void onMessage (WebSocket webSocket, String text) {
        listener.onMessageReceived (text);
    }

    @Override
    public void onMessage (WebSocket webSocket, ByteString bytes) {
        listener.onMessageReceived (bytes.toByteArray ());
    }

    @Override
    public void onClosing (WebSocket webSocket, int code, String reason) {

    }

    @Override
    public void onClosed (WebSocket webSocket, int code, String reason) {
        listener.onClose ();
    }

    @Override
    public void onFailure (WebSocket webSocket, Throwable t, Response response) {
        listener.onError (t);
    }
}